<template>
  <view class="cooperation">
    <view class="cooperation-banner">
      <image src="@/static/images/cooperate.png" mode=""></image>
    </view>
    <view class="cooperation-info">
      <view class="cooperation-form">
        <van-field :border="false" required @change="e => username = e.detail" :value="username" label="称呼"
          placeholder="请输入您的称呼" />
        <van-field :border="false" required @change="e => phone = e.detail" maxlength="11" :value="phone" label="您的手机号"
          placeholder="请输入您的手机号" />
        <van-field :border="false" required @change="e => smsCode = e.detail" maxlength="4" type="number" class="sms"
          v-model="smsCode" center clearable label="验证码" placeholder="请输入验证码" use-button-slot>
          <van-button :disabled="isCounting" @click="getVerificationCode" color="linear-gradient(#0366E0, #1E50DC)"
            slot="button" size="small" type="primary">
            {{ buttonLabel }}
          </van-button>
        </van-field>
        <van-cell-group :border="false">
          <view class="cooperation-btn">
            <van-button :disabled="btnDisabled" @click="handleApply" color="linear-gradient(#0366E0, #1E50DC)"
              type="info">马上申请
              立即赚钱</van-button>
          </view>
        </van-cell-group>
      </view>
      <view class="cooperation-img">
        <image class="invest" src="@/static/images/invest.png" mode=""></image>
      </view>
      <view class="cooperation-img">
        <image class="develop" src="@/static/images/develop.png" mode=""></image>
      </view>
    </view>
    <!-- 申请成功弹窗 -->
    <applySuccess :showApplyDialog="showApplyDialog" @handleClose="handleClose" @handleGetKnow="handleGetKnow"/>
  </view>
</template>

<script>
  import {
    getSmsCode,
    joinUs
  } from '@/api/user'
  import applySuccess from '@/component/applySuccess.vue'
  export default {
    data() {
      return {
        username: '',
        phone: '',
        smsCode: '',
        isCounting: false,
        count: 60,
        codeId: '',
        interval: null,
        showApplyDialog: false
      }
    },
    components: {
      applySuccess
    },
    computed: {
      btnDisabled() {
        const phonePattern = /^1[3-9]\d{9}$/; // 手机号正则表达式，仅支持中国大陆手机号
        const smsCodePattern = /^\d{4}$/; // 短信验证码正则表达式，6位数字
        // 校验逻辑
        if (!this.username) {
          return true;
        }
        if (!this.phone || !phonePattern.test(this.phone)) {
          return true;
        }
        if (!this.smsCode || !smsCodePattern.test(this.smsCode)) {
          return true;
        }
        return false;
      },
      buttonLabel() {
        return this.isCounting ? `${this.count}s` : '获取验证码';
      },
    },
    methods: {
      async getVerificationCode() {
        const phonePattern = /^1[3-9]\d{9}$/; // 手机号正则表达式，仅支持中国大陆手机号
        if (!this.phone) {
          uni.showToast({
            icon: 'none',
            title: '手机号必填'
          })
          return;
        }
        if (!phonePattern.test(this.phone)) {
          uni.showToast({
            icon: 'none',
            title: '手机号格式不正确'
          });
          return;
        }
        const data = await getSmsCode(this.phone)
        if (data.code === 200) {
          this.codeId = data.data
          // console.log(this.codeId);
          uni.showToast({
            title: '验证码已发送'
          })
          this.isCounting = true;
          this.count = 60;
          this.interval = setInterval(() => {
            this.count--;
            if (this.count === 0) {
              this.isCounting = false;
              clearInterval(this.interval);
            }
          }, 1000);
        }
      },
      async handleApply() {
        const obj = {
          name: this.username,
          phoneNumber: this.phone,
          codeId: this.codeId,
          code: this.smsCode
        }
        // console.log(obj);
        const data = await joinUs({
          ...obj
        })
        if (data.code === 200) {
          this.showApplyDialog = true
        } else {
          uni.showToast({
            icon: 'none',
            title: data.msg
          });
        }
      },
      handleClose() {
        this.showApplyDialog = false
      },
      handleGetKnow() {
        uni.navigateBack()
      }
    }
  }
</script>

<style lang="less" scoped>
  .cooperation {
    .cooperation-banner {
      width: 100%;
      height: 460px;

      image {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
    }

    .cooperation-info {
      padding: 0 10px 30px;
      background-color: #FF8201;
      margin-top: -46px;

      ::v-deep .van-cell-group {
        padding: 15px 0 0 0;
        background-color: #ffff;
        border-radius: 10px;
        border-bottom: unset !important;
      }

      .cooperation-form {
        position: relative;
        margin-bottom: 15px;
        background-color: #FFFFFF;
        border-radius: 10px;
        padding: 49rpx 0 43rpx 15rpx;
        overflow: hidden;

        ::v-deep .van-field__label {
          color: #222222 !important;
        }

        .sms {
          ::v-deep .van-button {
            border-radius: 10rpx !important;
            color: #3163EE;
            height: 50rpx !important;
          }
        }

        .cooperation-btn {
          display: flex;
          justify-content: center;
          margin-top: 10px;
          margin-right: 10px;

          ::v-deep .van-button {
            border-radius: 10px;
            color: #3163EE;
            height: 92rpx;
            width: 600rpx;
          }
        }
      }

      .cooperation-img {
        width: 100%;
        margin-bottom: 15px;

        .invest {
          width: 100%;
          height: 580rpx;
        }

        .develop {
          width: 100%;
          height: 441rpx;
        }
      }

      ::v-deep .van-cell {
        padding: 15rpx 20px !important;
        align-items: center;
      }

      ::v-deep .van-hairline {
        border-bottom: unset !important;
      }
    }

    ::v-deep .van-field__body {
      height: 78rpx;
      border: 1rpx solid #DBDBDB;
      padding: 0 5px 0 15px;
      box-sizing: border-box;
      border-radius: 10rpx;
    }

    ::v-deep .van-cell__title {
      max-width: 138rpx !important;
      min-width: 138rpx !important;
    }
  }
</style>